package com.xiao.sulingproduct.controller;

import com.xiao.sulingproduct.dao.CommodityDao;
import com.xiao.sulingproduct.entity.CommodityEntity;
import com.xiao.sulingproduct.fegin.EsFeign;
import com.xiao.sulingproduct.service.CommodityService;
import com.xiao.sulingproduct.vo.CommodityVo;
import com.xiao.sulingproduct.vo.ProductDerleteVo;
import com.xiao.sulingpublic.to.CommodityTo;
import com.xiao.sulingpublic.utils.PageUtils;
import com.xiao.sulingpublic.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 创建商品表
 *
 * @author wen
 * @email 1322127758@qq.com
 * @date 2022-03-11 14:46:16
 */
@RestController
@RequestMapping("sulingpxqg/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @Autowired
    private CommodityDao commodityDao;

    @Autowired
    private EsFeign esFeign;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("sulingpxqg:commodity:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = commodityService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 按ids 查询对应的商品价格
     */
    @RequestMapping("/list/byids")
    public R listByIds(@RequestBody List<Long> params){
        List<CommodityEntity> commodityList = commodityDao.listByIds(params);

        List<BigDecimal> collect = commodityList.stream().map(CommodityEntity::getCoPrice).collect(Collectors.toList());

        return R.ok().put("prices", collect);
    }

    /**
     * 按商品id集合 查询对应的商品数据
     */
    @RequestMapping("/list/commoditylist")
    public R getCommodityList(@RequestBody List<Long> params){
        List<CommodityEntity> commodityList = commodityDao.listByIds(params);

        List<CommodityTo> commodityTos = commodityList.stream().map(ite -> {
            CommodityTo commodityTo = new CommodityTo();
            BeanUtils.copyProperties(ite, commodityTo);
            return commodityTo;
        }).collect(Collectors.toList());
        return R.ok().put("commodityTos", commodityTos);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{coId}")
    //@RequiresPermissions("sulingpxqg:commodity:info")
    public R info(@PathVariable("coId") Long coId){
		CommodityEntity commodity = commodityService.getById(coId);

        return R.ok().put("commodity", commodity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("sulingpxqg:commodity:save")
    public R save(@RequestBody CommodityEntity commodity){
		commodityService.save(commodity);

        return R.ok();
    }

    /**
     * 保存
     */
    @PostMapping("/save/commodity")
    public R saveCommodity(CommodityVo commodityVo){
        R save;
        if(null != commodityVo){
            save = commodityService.saveCommodity(commodityVo);

            return save;
        }

        return R.error("保存失败");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("sulingpxqg:commodity:update")
    public R update(@RequestBody CommodityEntity commodity){


		commodityService.updateById(commodity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("sulingpxqg:commodity:delete")
    public R delete(@RequestBody Long[] coIds){
		commodityService.removeByIds(Arrays.asList(coIds));

        return R.ok();
    }

    /**
     * 删除
     */
    @Transactional
    @RequestMapping("/delete/product")
    public R deleteProduct(@RequestBody ProductDerleteVo productDerleteVo){
        Long coId = productDerleteVo.getCoId();
        // 删除数据
        commodityService.removeById(coId);

        //删除es中的数据
        esFeign.delete(coId);

        return R.ok();
    }


}
